<?php
Class tabel_paging{
	
	private $conn;
	private $sql_count;
	private $rows_per_page;
	private $sql_result;
	private $headerBody;
	private $totalCount;
	
	public function __construct($con,$sql_count,$rows_per_page,$sql_result,$headerBody){
		$this->conn = $con;
		$this->sql_count = $sql_count;
		$this->rows_per_page = $rows_per_page;
		$this->sql_result = $sql_result;
		$this->headerBody = $headerBody;
	}
	
	public function actionDel($action,$value,$change){
		//$actionVal = $action.$value;
		$actionVal = str_replace($change,$value,$action);
		return $actionVal; 
	}
	
	public function count_rows($conn,$sql_count) {
	    $stmt = ociparse($conn,$sql_count);
	    ociexecute($stmt);
	    ocifetch($stmt);
		return OCIResult($stmt,'NUM_ROWS');
	}
	
	public function total_pages($total_rows, $rows_per_page) {
	    if ( $total_rows < 1 ) $total_rows = 1;
	    return ceil($total_rows/$rows_per_page);
	}

	public function page_to_row($current_page, $rows_per_page) {
	    $start_row = ($current_page-1) * $rows_per_page + 1;
	    return $start_row;
	}
	
	public function draw_pager($url, $total_pages, $current_page) {
		$action_tool = (isset($_REQUEST['auto']) && $_REQUEST['auto'] == true)?true:false;
		
	    if ( $current_page <= 0 || $current_page > $total_pages ) {
	        $current_page = 1;
	    }
	    if ( $current_page > 1) {
	    	if($action_tool){
	        printf( "<a href='$url?page=%d&auto=true'>[Start]</a> \n" , 1);
	        printf( "<a href='$url?page=%d&auto=true'>[Prev]</a> \n" , ($current_page-1));	    		
	    	}else{
	        printf( "<a href='$url?page=%d'>[Start]</a> \n" , 1);
	        printf( "<a href='$url?page=%d'>[Prev]</a> \n" , ($current_page-1));	    	
	    	}
	    }
	    for( $i = ($current_page-3); $i <= $current_page+3; $i++ ) {
	        if ($i < 1) continue;
	        if ( $i > $total_pages ) break;
	        if($action_tool){	        	
		        if ( $i != $current_page ) {
		            printf( "<a href='$url?page=%1\$d&auto=true' style=\"color:#0000CC\">%1\$d</a> \n" , $i);
		        } else {
		            printf("<a href='$url?page=%1\$d&auto=true' style=\"color: #FF0000\"><strong>%1\$d</strong></a> \n",$i);
		        }
	        }else{
		        if ( $i != $current_page ) {
		            printf( "<a href='$url?page=%1\$d' style=\"color:#0000CC\">%1\$d</a> \n" , $i);
		        } else {
		            printf("<a href='$url?page=%1\$d' style=\"color: #FF0000\"><strong>%1\$d</strong></a> \n",$i);
		        }	        	
	        }
	    }
	    if ( $current_page < $total_pages) {
	    	if($action_tool){
	        printf( "<a href='$url?page=%d&auto=true'>[Next]</a> \n" , ($current_page+1));
	        printf( "<a href='$url?page=%d&auto=true'>[End]</a> \n" , $total_pages);
	    }else{
	        printf( "<a href='$url?page=%d'>[Next]</a> \n" , ($current_page+1));
	        printf( "<a href='$url?page=%d'>[End]</a> \n" , $total_pages);	    	
	    }
	    }
	}
	
	private function cekData($key,$val){
		$type = strtoupper(substr(trim($key),0,4));
		switch($type){
			case ":STR" :
				return strval($val); 
			break;
			case ":INT" :
				return intval($val);
			break;
			default:
				return $val;
			break;
		}
	}
	
	public function paged_result($conn,$current_page,$rows_per_page,$sql_result,$headerBody,$form) {
		$action_tool = (isset($_REQUEST['auto']) && $_REQUEST['auto'] == true)?false:true;		
		$query_substr = substr($sql_result['sql_result'], 0,5);
		$res = array();		
		if(strtoupper(($query_substr)) == 'BEGIN'){
			$curs = oci_new_cursor($conn);
			$stmt = oci_parse($conn,$sql_result['sql_result']);
		    foreach($sql_result['condition'] as $key => $val){		    	  		    	
				if($key != ':cursor'){
		    		oci_bind_by_name($stmt, $key, $this->cekData($key,$val));		    						
				}else{
					oci_bind_by_name($stmt,$key,&$curs,-1,OCI_B_CURSOR);					
				}
		    }			
   			oci_execute($stmt,OCI_DEFAULT);
   			oci_execute($curs,OCI_DEFAULT);
			while ($data = oci_fetch_array($curs, OCI_ASSOC)){
				$res[] = $data;
			}
			$resultArray = count($res);
		}else{
		    $stmt = oci_parse($conn,$sql_result['sql_result']);
		    foreach($sql_result['condition'] as $key => $val){		    	  		    	
				oci_bind_by_name($stmt, $key, $this->cekData($key,$val));		    	
		    }		    
		    oci_execute($stmt);			
			$resultArray = oci_fetch_all($stmt,$res,null, null, OCI_FETCHSTATEMENT_BY_ROW);
		}
	if($resultArray == 0){
			echo 'Tidak ditemukan';
	}else{		
	echo "<table class='product-table' width=100% border=1 cellspacing=2 cellpading=1 align=centre style='margin-bottom: 10px;'><tbody align='centre'>";
	echo "<tr>";
			
	if($action_tool){		
		echo "<th class='table-header-repeat line-left'><a href='javascript:void(0)'>NO</a></td>";
	}else{
		echo "<th class='table-header-repeat line-left'><a href='javascript:void(0)'>PICK</a></td>";	
	}
	
	foreach($headerBody['TABLE'] as $key => $val){
		echo "<th class='table-header-repeat line-left'><a href='javascript:void(0)'>".$key."</a></td>";
	}
		if($action_tool){
		echo "<th class='table-header-repeat line-left'><a href='javascript:void(0)'>ACTION</a></td>";
		}
		
	    $i = (isset($current_page) && $current_page > 1)?$current_page:1;
	
	    foreach($res as $key => $val){
			echo "<tr id='row_".$val[$headerBody['OTHER']['ID']]."'>";
			if($action_tool){
				echo "<td>".$i."</td>";				
			}else{				
				foreach($headerBody['OTHER'] as $a => $b){
					$cekList[$i][] = '"'.$val[$b].'"';
					$joinAll[$i] = implode(',',$cekList[$i]);
										
				}											
				echo "<td><input type='checkbox' onclick='return cekValue($joinAll[$i])'/></td>";							
			}
			
			foreach($headerBody['TABLE'] as $key => $body){
				echo "<td>".$val[$body]."</td>";
			}
				if($action_tool){
					echo "<td>";
				if($action_tool){
	      			echo "<a class='btn' href='$form?form=update&update=".$val[$headerBody['OTHER']['ID']]."'><img src='http://".IMAGE_PATH."gtk-edit.png'/></a>";      		
				}
				if($action_tool && count($headerBody['OTHER']['DELETE']) > 0){
					$value = $this->actionDel($headerBody['OTHER']['DELETE']['ACTION'],$val[$headerBody['OTHER']['ID']],"%ID%");
					echo "<a class='btn' id='row_".$val[$headerBody['OTHER']['ID']]."' href='javascript:void(0)' $value><img src='http://".IMAGE_PATH."delete.png'/></a>";	      							
				}
				echo "</td>";
				}
	      echo "</tr>";
	      $i++;
	    }	
			echo "</tbody></table>";
	   }
	}
	
}